package com.paireach.bw.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RatingBar;

import com.baidu.trace.TraceLocation;
import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.interface2.LocateCallback;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;

import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2017/2/16.
 */

public class ServiceEvaluationActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "SafeCheckActivity";
    private Button btn;
    private Toolbar toolbar;
    private RatingBar rb1;
    private RatingBar rb2;
    private RatingBar rb3;
    private String orderCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition slide_bottom = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_left);
            getWindow().setEnterTransition(slide_bottom);
        }
        setContentView(R.layout.activity_service_evaluation);
        orderCode = getIntent().getStringExtra("orderCode");
        findViews();
        initViews();
    }

    private void initViews() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        toolbar.setTitle("服务评价");
        btn.setOnClickListener(this);
    }

    private void findViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        rb1 = (RatingBar) findViewById(R.id.ratingBar1);
        rb2 = (RatingBar) findViewById(R.id.ratingBar2);
        rb3 = (RatingBar) findViewById(R.id.ratingBar3);
        btn = (Button) findViewById(R.id.btn_commit);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_commit:
                final int rating1 = (int) rb1.getRating();
                final int rating2 = (int) rb2.getRating();
                final int rating3 = (int) rb3.getRating();
                if (rating1 != 0 && rating2 != 0 && rating3 != 0) {
                    showProgressDialog();
                    LocationInfo location = ((MyApplication) getApplication()).locationInfo;
                    new WayBillRequest().commitServiceEvaluation(phoneNumber, orderCode, rating1, rating2, rating3, location.getLongitude() + "", location.getLatitude() + "",
                            driverNum);
                } else {
                    showToast("请全部评价");
                }
                break;
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.COMMIT_SERVICE_EVALUATION) {
            jsonParse(event.getJson());
        }
    }

    private void jsonParse(String json) {
        ReturnInfo info = GsonUtil.jsonToBean(json, ReturnInfo.class);
        showToast(info.getMsg());
        AppEvent event = new AppEvent();
        event.setTag("to_frag3");
        EventBus.getDefault().post(event);
        finish();
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.COMMIT_SERVICE_EVALUATION) {
            showToast("提交评价失败");
        }
    }
}
